<?php
###########################################################################
#  Copyright (C) 2006-2007 Glyphix, Inc. <briz@glyphix.com>               #
#                                                                         #
#  Permission is hereby granted, free of charge, to any person obtaining  #
#  a copy of this software and associated documentation files (the        #
#  "Software"), to deal in the Software without restriction, including    #
#  without limitation the rights to use, copy, modify, merge, publish,    #
#  distribute, sublicense, and/or sell copies of the Software, and to     #
#  permit persons to whom the Software is furnished to do so, subject to  #
#  the following conditions:                                              #
#                                                                         #
#  The above copyright notice and this permission notice shall be         #
#  included in all copies or substantial portions of the Software.        #
#                                                                         #
#  Except as contained in this notice, the name(s) of the above           #
#  copyright holders shall not be used in advertising or otherwise to     #
#  promote the sale, use or other dealings in this Software without       #
#  prior written authorization.                                           #
#                                                                         #
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        #
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     #
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. #
#  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR      #
#  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,  #
#  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  #
#  OTHER DEALINGS IN THE SOFTWARE.                                        #
###########################################################################

/**
 * GXCookie
 *
 * Central cookie management class. It can store and retrieve any PHP data type.
 * @author		Brad Brizendine <briz@glyphix.com>
 * @link		http://www.glyphix.com/
 * @license		http://opensource.org/licenses/bsd-license.php BSD License
 * @version		1.0
 * @package		GXPage
 * @subpackage	Helpers
 * @copyright	Copyright 2006 - 2007, Glyphix, Inc.
 * @uses		SingletonPattern
 */

class GXCookie extends SingletonPattern {

	/**
	 * getInstance
	 *
     * Returns the single unique instance of this class.
	 * @return object unique instance of this object
     */
	public static function getInstance(){
		return SingletonPattern::getSingleton( __CLASS__ );
	}

	/**
	 * get
	 *
	 * Retrieves the value of a cookie.
	 * The value is base64 decoded and unserialized, so it will be returned in its native PHP format.
	 * @param string $name the name of the cookie
	 * @return mixed the value of the cookie
	 */
	public function get( $name = null ){
		if( !$name ){
			trigger_error( 'Missing name', E_USER_ERROR );
			return false;
		}

		// get the value or set it to null
		$value = (isset($_COOKIE[$name])) ? unserialize( base64_decode($_COOKIE[$name]) ) : null;
		if( !$value ){
			// if we have an original value, must have been a problem decoding, so just return raw value
			if(isset($_COOKIE[$name]) && strlen($_COOKIE[$name])){
				$value = $_COOKIE[$name];
			}else{
				trigger_error( 'Cookie ' .$name .' has no value', E_USER_WARNING );
				return false;
			}
		}

		// log the value and return it
		trigger_error( 'Retrieving cookie value for ' .$name .': got ' .$value, E_USER_WARNING );
		return $value;
	}

	/**
	 * set
	 *
	 * Sets a cookie.
	 * The value is serialized and base64 encoded, so any php data type can be stored.
	 * @param string $name the name of the cookie
	 * @param string $value the value of the cookie
	 * @param string $duration the number of seconds the cookie will last (0 or null is a session cookie)
	 * @return boolean
	 */
	public function set( $name = null, $value = null, $duration = null ){
		if( !$name ){
			trigger_error( 'Missing name', E_USER_ERROR );
			return false;
		}

		// if headers have already been sent, log an error
		if( headers_sent($file,$line) ){
			trigger_error( 'Headers have already been sent in ' .$file .'::' .$line .'; cannot set cookie value', E_USER_ERROR );
			return false;
		}

		// set the duration if we have a value for it
		if($duration){
			$duration = time() + $duration;
		}

		// if we have a value, assume it's encoded and serialized
		if( $value ){
			$value = base64_encode( serialize($value) );
		}else{
			$value = null;
		}

		// log the resulting value
		trigger_error( 'Setting cookie value ' .$name .'="' .$value .'"', E_USER_WARNING );
		return setcookie( $name, $value, $duration, '/' );
	}

	/**
	 * delete
	 *
	 * Deletes a cookie by setting its expire value to a negative value.
	 * @param string $name the name of the cookie to delete
	 * @return boolean
	 */
	public function delete( $name = null ){
		if( !$name ){
			trigger_error( 'Missing name', E_USER_ERROR );
			return false;
		}

		// if headers have already been sent, log an error
		if( headers_sent($file,$line) ){
			trigger_error( 'Headers have already been sent in ' .$file .'::' .$line .'; cannot set cookie value', E_USER_ERROR );
			return false;
		}

		// set duration in the past
		return $this->set( $name, null, time()-600 );
	}

}

?>